/*
  Copyright (c) 2002, 2003 Russell Morris
  All rights reserved.

  Redistribution and use in source and binary forms, with or without modification, 
  are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above copyright notice, 
      this list of conditions and the following disclaimer. 
      
    * Redistributions in binary form must reproduce the above copyright notice, 
      this list of conditions and the following disclaimer in the documentation 
      and/or other materials provided with the distribution. 
      
    * The names of the contributors may not be used to endorse or promote 
      products derived from this software without specific prior written permission. 
      
    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 
    THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.CodeDom;
using System.Collections;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Reflection;

using System.Windows.Forms;

namespace WRM.Windows.Forms.Design
{
	/// <summary>
	///   A CodeDomSerializer built to serialize out objects that contain
	///   <see cref="PaneNodeCollection">PaneNodeCollection</see>s
	/// </summary>
	internal class PaneNodeCollectionSerializer : CodeDomSerializer
	{
    /// <summary>
    /// Creates an instance of <c>PaneNodeCollectionSerializer</c>
    /// </summary>
		public PaneNodeCollectionSerializer()
		{
		}

    /// <summary>
    /// Deserialize a 
    /// <see cref="PaneNodeCollection">PaneNodeCollection</see>-containing
    /// class from source code.
    /// </summary>
    /// <param name="manager">
    /// The VS.NET-supplied serialization manager
    /// </param>
    /// <param name="codeObject">
    /// The object representing the loaded and parsed source code from
    /// which this component should be deserialized
    /// </param>
    /// <returns>
    /// Returns an instance of the component it is supposed to deserialize
    /// </returns>
    /// <remarks>
    /// The function simply lets the <paramref name="manager"/>-supplied 
    /// base serializer to actually do the deserialization.
    /// </remarks>
    public override object Deserialize(
      IDesignerSerializationManager manager, 
      object codeObject)
    {
      CodeDomSerializer srlzr = GetBaseSerializer(manager);
      return srlzr.Deserialize(manager,codeObject);
    }

    /// <summary>
    /// Serialize a 
    /// <see cref="PaneNodeCollection">PaneNodeCollection</see>-containing
    /// class to source code.
    /// </summary>
    /// <param name="manager">
    /// The VS.NET-supplied serialization manager
    /// </param>
    /// <param name="value">
    /// The component instance that should be serialized to source code
    /// </param>
    /// <returns>
    /// Returns a <see cref="CodeStatementCollection"/> containing 
    /// source code statements that represent <paramref name="value"/> in
    /// source-code form.
    /// </returns>
    public override object Serialize(
      IDesignerSerializationManager manager, 
      object value)
    {
      object codeObject = GetBaseSerializer(manager).Serialize(manager, value);
      ArrayList topLevelPanes = new ArrayList();
      
      CodeStatementCollection csc = (CodeStatementCollection)codeObject;

      PropertyDescriptor paneNodesProp = TypeDescriptor.GetProperties(value)["PaneNodes"];
      PaneNodeCollection nodes = (PaneNodeCollection)paneNodesProp.GetValue(value);
      string compName = manager.GetName(value);

      // Create the call to PaneNodes.Add(...) for each PaneNode
      foreach(PaneNode child in nodes)
      {
        CodeThisReferenceExpression thisRef = 
          new CodeThisReferenceExpression();
        CodeFieldReferenceExpression fieldRef = 
          new CodeFieldReferenceExpression(thisRef,compName);
        CodeFieldReferenceExpression childRef = 
          new CodeFieldReferenceExpression(thisRef,manager.GetName(child.PropertyPane));
        CodePropertyReferenceExpression paneNodesRef = 
          new CodePropertyReferenceExpression(fieldRef,"PaneNodes");
        CodeMethodInvokeExpression invokeExpr = new CodeMethodInvokeExpression(
          paneNodesRef,
          "Add",
          childRef,
          new CodePrimitiveExpression(child.Index),
          new CodePrimitiveExpression(child.ImageIndex),
          new CodePrimitiveExpression(child.SelectedImageIndex));

        csc.Add(invokeExpr);
      }

      return codeObject;
    }
  
    /// <summary>
    /// Get's the base serializer object for the base class of PropertyTree.
    /// </summary>
    /// <param name="manager">The VS.NET supplied serialization manager</param>
    /// <returns>
    /// Returns a <see cref="CodeDomSerializer"/> that is capable of deserializing
    /// the <see cref="PropertyTree"/>.
    /// </returns>
    private CodeDomSerializer GetBaseSerializer(IDesignerSerializationManager manager)
    {
      return (CodeDomSerializer)manager.GetSerializer(
        typeof(PropertyTree).BaseType, 
        typeof(CodeDomSerializer));
    }
  }
}
